const Sequelize = require('sequelize');
const moment = require('moment');
const hooks = require('./sequelizeHooks');

module.exports = function (sequelize, DataTypes) {
  const fields = {
    id: {
      autoIncrement: true,
      type: DataTypes.BIGINT,
      allowNull: false,
      primaryKey: true,
      comment: '物料分类id'
    },
    uid: {
      type: DataTypes.BLOB,
      allowNull: true,
      comment: 'uid'
    },
    tenant_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: 0,
      primaryKey: true,
      comment: '租户id'
    },
    catalog_code: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '分类编码'
    },
    catalog_name: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: '分类名称'
    },
    parent_id: {
      type: DataTypes.BIGINT,
      allowNull: true,
      defaultValue: 0,
      comment: '上级id'
    },
    code_rule: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '编码规则'
    },
    model_template: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '物料规格模板'
    },
    sort: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: '排序'
    },
    sell: {
      type: DataTypes.SMALLINT,
      allowNull: true,
      defaultValue: 0,
      comment: '是否上架销售，0-否 1-是'
    },
    history: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '历史'
    },
    create_time: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: '创建时间'
    },
    create_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '创建人ID'
    },
    create_user_name: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '创建人姓名'
    },
    modify_time: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: '修改时间'
    },
    modify_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '修改人id'
    },
    modify_user_name: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '修改人姓名'
    },
    create_date: {
      type: DataTypes.VIRTUAL,
      get () {
        const val = this.getDataValue('create_time');
        return val && moment(val, 'X').format('YYYY/MM/DD');
      }
    },
    modify_date: {
      type: DataTypes.VIRTUAL,
      get () {
        const val = this.getDataValue('modify_time');
        return val && moment(val, 'X').format('YYYY/MM/DD');
      }
    }
  };

  return sequelize.define('t_material_catalog', fields, {
    sequelize,
    tableName: 't_material_catalog',
    hasTrigger: true,
    timestamps: false,
    freezeTableName: true,
    indexes: [
      {
        name: 'PRIMARY',
        unique: true,
        using: 'BTREE',
        fields: [
          { name: 'id' },
          { name: 'tenant_id' }
        ]
      },
      {
        name: 'idx_uid',
        using: 'BTREE',
        fields: [
          { name: 'uid' }
        ]
      },
      {
        name: 'idx_tenant_id',
        using: 'BTREE',
        fields: [
          { name: 'tenant_id' }
        ]
      },
      {
        name: 'idx_parent_id',
        using: 'BTREE',
        fields: [
          { name: 'parent_id' }
        ]
      }
    ],
    hooks: {
      beforeCreate (record, options) {
        hooks.singleBeforeCreate(record, options);
      },
      beforeUpdate (record, options) {
        hooks.singleBeforeUpdate(record, options, fields);
      }
    }
  });
};
